<template>
  <!-- dialog -->
  <el-dialog :title="title" :visible.sync="dialogFormVisible">
    <el-form :model="resourceData" v-loading="isLoading">
      <el-form-item label="资源名称" :label-width="formLabelWidth">
        <el-input v-model="resourceData.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="资源路径" :label-width="formLabelWidth">
        <el-input v-model="resourceData.url" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="资源分类" :label-width="formLabelWidth">
        <el-select
          v-model="resourceData.categoryId"
          placeholder="请选择活动区域"
        >
          <el-option
            v-for="item in categorys"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="描述" :label-width="formLabelWidth">
        <el-input type="textarea" v-model="resourceData.description"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button>取 消</el-button>
      <el-button :disabled="isLoading" type="primary" @click="onSubmit"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { getAllResourceCategory } from "@/api/resource-category";
import { getResource, resourceSaveOrUpdate } from "@/api/resource";
export default {
  name: "ResourceCreateOrEdit",
  data() {
    return {
      isCreate: true, //标识是创建or编辑
      dialogFormVisible: false,
      resourceData: {
        id: undefined,
        name: "",
        url: "",
        categoryId: 1,
        description: "",
      },
      categorys: [],
      isLoading: false,
      formLabelWidth: "120px",
    };
  },
  computed: {
    title() {
      return this.isCreate ? "新建资源" : "编辑资源";
    },
  },
  methods: {
    //弹出对话框
    showAndInit(resourceId = 0) {
      //调用方法就显示
      this.dialogFormVisible = true;
      //判断是创建还是编辑（resourceId = 0就为false,12345就是编辑）
      //   this.isCreate = !resourceId;
      this.isCreate = resourceId < 1; //创建

      //获取资源类别
      getAllResourceCategory().then((res) => {
        if (res.data.code === "000000") {
          this.categorys = res.data.data;
        }
      });

      //新建或编辑(逻辑代码)
      if (this.isCreate) {
        //为true,新建(清空)
        this.resourceData = {
          name: "",
          url: "",
          categoryId: 1,
          description: "",
        };
      } else {
        //编辑(调接口获取数据,把要编辑的资源显示出来)
        this.isLoading = true;
        getResource(resourceId)
          .then((res) => {
            if (res.data.code === "000000") {
              this.resourceData = res.data.data;
            }
            console.log("资源列表-编辑-对应指定id内容", res);
          })
          .finally(() => {
            this.isLoading = false;
          });
      }
    },
    //表单提交动作
    onSubmit() {
      resourceSaveOrUpdate(this.resourceData)
        .then((res) => {
          if (res.data.data) {
            this.$message.success(`${this.title}成功`);
            this.$emit('resource-change')
          } else {
            this.$message.error(`${this.title}失败~`);
          }
        })
        .finally(() => {
          this.dialogFormVisible = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped></style>
